/**
 * 发送消息给service_worker
 * @param {Object} message - 要发送的消息对象
 * @param {Function} callback - 回调函数
 */
function sendMessageToServiceWorker(message, callback) {
    chrome.runtime.sendMessage(message, function(response) {
        if (callback) {
            callback(response);
        }
    });
}

// 开始按钮点击事件
document.getElementById('run').addEventListener('click', function() {
    const timeInput = document.getElementById('time');
    const time = parseInt(timeInput.value) || 15; // 默认15秒
    
    // 先设置时间
    sendMessageToServiceWorker({ action: 'setTime', time: time }, function(response) {
        console.log('时间设置响应:', response);
    });
    
    // 然后开始切换
    sendMessageToServiceWorker({ action: 'start' }, function(response) {
        console.log('开始切换响应:', response);
    });
});

// 停止按钮点击事件
document.getElementById('stop').addEventListener('click', function() {
    sendMessageToServiceWorker({ action: 'stop' }, function(response) {
        console.log('停止切换响应:', response);
    });
});
